Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added only to Model #866

Merged
merged 7 commits into from
Jan 4, 2024

Conversation

JarriqTheTechie
Copy link
Contributor

Added

  • Adds only method to model class. This method allows you to get a subset of the model's attributes.

Usage

User.first_or_create(
      {"email": "[email protected]"},
      {"email": "[email protected]", "password": "password123"}
  ).only([
      "id",
      "email",
  ])

this would return the following

{'id': 1, 'email': '[email protected]'}

In addition to the above usage, you can also use this as a way to remap data. We can alias attributes so that the results can match its destination. Example usage

User.first_or_create(
      {"email": "[email protected]"},
      {"email": "[email protected]", "password": "password123"}
  ).only([
      "id as UserIdentifier",
      "email as EmailAddress",
  ])

this would return the following

{'UserIdentifier': 2, 'EmailAddress': '[email protected]'}

jrolle and others added 6 commits December 9, 2023 13:10
…when mapping return data into new datasources which expect different naming conventions. This could be done at the database query level but is much easier to do using this method and only adds few extra LOC.
@josephmancuso
Copy link
Member

made a small change here if you just want to pull 1 column by not needing to pass a list

User.first_or_create(
      {"email": "[email protected]"},
      {"email": "[email protected]", "password": "password123"}
  ).only("email")

@josephmancuso
Copy link
Member

and i added tests

@josephmancuso josephmancuso merged commit 98a6393 into MasoniteFramework:2.0 Jan 4, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants